cmake_minimum_required(VERSION 3.26)
project(TestFindHDF5 LANGUAGES ${TEST_LANGS})
include(CTest)
message(STATUS "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)")

if(TEST_PARALLEL)
  set(HDF5_PREFER_PARALLEL 1)
endif()

find_package(HDF5 REQUIRED COMPONENTS ${TEST_LANGS} HL)

set(variables HDF5_FOUND HDF5_VERSION)
if(NOT TEST_WRAPPER_AS_COMPILER)
   list(APPEND variables HDF5_INCLUDE_DIRS HDF5_LIBRARIES HDF5_HL_LIBRARIES)
   foreach(lang ${TEST_LANGS})
     list(APPEND variables
       HDF5_${lang}_COMPILER_EXECUTABLE
       HDF5_${lang}_INCLUDE_DIRS
       HDF5_${lang}_LIBRARIES
       HDF5_${lang}_HL_LIBRARIES
       )
   endforeach()
 endif()
foreach(var IN LISTS variables)
  message(STATUS "${var}='${${var}}'")
endforeach()
foreach(var IN LISTS variables)
  if(NOT DEFINED ${var})
    message(SEND_ERROR "Variable '${var}' is not defined!")
  endif()
endforeach()

set(targets HDF5::HDF5)
if(CMAKE_C_COMPILER_LOADED)
  list(APPEND targets hdf5::hdf5 hdf5::hdf5_hl)
endif()
if(CMAKE_CXX_COMPILER_LOADED)
  list(APPEND targets hdf5::hdf5_cpp hdf5::hdf5_hl_cpp)
endif()
if(CMAKE_Fortran_COMPILER_LOADED)
  list(APPEND targets hdf5::hdf5_fortran hdf5::hdf5_hl_fortran)
endif()
foreach(target IN LISTS targets)
  if(NOT TARGET ${target})
    message(SEND_ERROR "Target '${target}' not defined!")
  endif()
endforeach()

message(STATUS "HDF5_IS_PARALLEL='${HDF5_IS_PARALLEL}'")
if(TEST_SERIAL)
  if(HDF5_IS_PARALLEL)
    message(SEND_ERROR "HDF5_IS_PARALLEL is true in serial test.")
  endif()
endif()
if(TEST_PARALLEL)
  if(NOT HDF5_IS_PARALLEL)
    message(SEND_ERROR "HDF5_IS_PARALLEL is false in parallel test.")
  endif()
endif()

if(TEST_PARALLEL)
  set(MPI_CXX_SKIP_MPICXX TRUE)
  set(MPI_CXX_VALIDATE_SKIP_MPICXX TRUE)
  find_package(MPI REQUIRED)
  set(mpi_C_tgt MPI::MPI_C)
  set(mpi_C_inc ${MPI_C_INCLUDE_PATH})
  set(mpi_C_lib ${MPI_C_LINK_FLAGS} ${MPI_C_LIBRARIES})
  set(mpi_Fortran_tgt MPI::MPI_Fortran)
  set(mpi_Fortran_inc ${MPI_Fortran_INCLUDE_PATH})
  set(mpi_Fortran_lib ${MPI_Fortran_LIBRARIES})
endif()

if(CMAKE_C_COMPILER_LOADED)
  add_executable(test_C_tgt test_C.c)
  target_link_libraries(test_C_tgt PRIVATE hdf5::hdf5 ${mpi_C_tgt})
  add_test(NAME test_C_tgt COMMAND test_C_tgt test_C_tgt.h5)

  add_executable(test_C_var test_C.c)
  target_include_directories(test_C_var PRIVATE ${HDF5_C_INCLUDE_DIRS} ${mpi_C_inc})
  target_link_libraries(test_C_var PRIVATE ${HDF5_C_LIBRARIES} ${mpi_C_lib})
  add_test(NAME test_C_var COMMAND test_C_var test_C_var.h5)
endif()

if(CMAKE_CXX_COMPILER_LOADED)
  add_executable(test_CXX_tgt test_CXX.cxx)
  target_link_libraries(test_CXX_tgt PRIVATE hdf5::hdf5_cpp
    hdf5::hdf5 # FIXME: hdf5::hdf5_cpp should link hdf5::hdf5 automatically.
    ${mpi_C_tgt})
  add_test(NAME test_CXX_tgt COMMAND test_CXX_tgt test_CXX_tgt.h5)

  add_executable(test_CXX_var test_CXX.cxx)
  target_include_directories(test_CXX_var PRIVATE ${HDF5_CXX_INCLUDE_DIRS} ${mpi_C_inc})
  target_link_libraries(test_CXX_var PRIVATE ${HDF5_CXX_LIBRARIES} ${mpi_C_lib})
  add_test(NAME test_CXX_var COMMAND test_CXX_var test_CXX_var.h5)
endif()

if(CMAKE_Fortran_COMPILER_LOADED)
  add_executable(test_Fortran_tgt test_Fortran.f90)
  target_link_libraries(test_Fortran_tgt PRIVATE hdf5::hdf5_fortran ${mpi_Fortran_tgt})
  add_test(NAME test_Fortran_tgt COMMAND test_Fortran_tgt)

  add_executable(test_Fortran_var test_Fortran.f90)
  target_include_directories(test_Fortran_var PRIVATE ${HDF5_Fortran_INCLUDE_DIRS} ${mpi_Fortran_inc})
  target_link_libraries(test_Fortran_var PRIVATE ${HDF5_Fortran_LIBRARIES} ${mpi_Fortran_lib})
  add_test(NAME test_Fortran_var COMMAND test_Fortran_var)
endif()
